package com.haidao.admin.component.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.haidao.admin.component.dao.ReportPlatformInfoDao;
import com.haidao.admin.component.entity.ReportOrgInfo;
import com.haidao.admin.component.entity.ReportPlatformInfo;
import com.haidao.admin.component.params.ReportParam;
import com.opencsv.CSVWriter;
import com.opencsv.bean.CsvToBean;
import com.opencsv.bean.CsvToBeanBuilder;
import com.opencsv.bean.StatefulBeanToCsv;
import com.opencsv.bean.StatefulBeanToCsvBuilder;
import com.opencsv.exceptions.CsvDataTypeMismatchException;
import com.opencsv.exceptions.CsvRequiredFieldEmptyException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("report_platform_info")
@Slf4j
public class ReportPlatformInfoServiceImpl extends AbstractReportService<ReportPlatformInfo> {
    @Resource
    private ReportPlatformInfoDao reportPlatformInfoDao;
    @Value("${report.baseUrl}")
    private String baseUrl;

    @Value("${report.cmcId}")
    private String cmcId;

    @Override
    protected List<ReportPlatformInfo> selectList() {
        return reportPlatformInfoDao.findByReportState(0);
    }

    @Override
    protected JSONObject buildRequestBody(ReportPlatformInfo bean, String cmcID) {
        JSONObject reqBody = new JSONObject(bean);
        reqBody.set("cmcID", cmcID);
        return reqBody;
    }

    @Override
    protected ReportPlatformInfo selectById(int id) {
        return reportPlatformInfoDao.findById(id).get();
    }

    @Override
    public void save(InputStreamReader reader) {
        CsvToBean<ReportPlatformInfo> csvToBean = new CsvToBeanBuilder<ReportPlatformInfo>(reader)
                .withType(ReportPlatformInfo.class)
                .build();
        List<ReportPlatformInfo> list = csvToBean.parse();
        for (ReportPlatformInfo daus : list) {
            reportPlatformInfoDao.save(daus);
        }
    }

    @Override
    public Page<ReportPlatformInfo> selectAsset(ReportParam param) {
        return reportPlatformInfoDao.findAll(PageRequest.of(param.getPageNumber()- 1, param.getPageSize()));
    }

    @Override
    public void export(Writer writer){
        int page=0;
        String url="";// TODO
        Map<String,Object> params=new HashMap<>();
        params.put("cmcId",cmcId);
        params.put("pageSize",100);
        List<ReportPlatformInfo> beans = new ArrayList<>();
        while (true){
            try {
                params.put("pageNum",page++);
                String ret = HttpUtil.get(baseUrl+url,params);
                JSONObject resp = new JSONObject(ret);
                List<ReportPlatformInfo> list = resp.getBeanList("content",ReportPlatformInfo.class);
                if(CollectionUtil.isEmpty(list)){
                    break;
                }else {
                    beans.addAll(list);
                }
            }catch (Exception ex) {
                ex.printStackTrace();
                break;
            }
        }
        CSVWriter csvWriter = new CSVWriter(writer);
        StatefulBeanToCsv<ReportPlatformInfo> beanToCsv = new StatefulBeanToCsvBuilder<ReportPlatformInfo>(csvWriter)
                .withQuotechar(CSVWriter.NO_QUOTE_CHARACTER)
                .build();
        try {
            beanToCsv.write(beans);
            csvWriter.close();
        } catch (IOException | CsvDataTypeMismatchException | CsvRequiredFieldEmptyException e) {
            e.printStackTrace();
        }
    }
}
